<?php
class Silk_Zgb_Block_Product_Sales extends Mage_Catalog_Block_Product_List
{   
	protected $hotProductCategoryId = 14;
	protected $newProductCategoryId = 135;

	public function getHotProducts()
	{
		return $this->getExplosion();
		$productCollection = Mage::getModel('catalog/category')->load($this->hotProductCategoryId)
				->getProductCollection()
			    ->addAttributeToSelect('*')
			    ->addAttributeToFilter('status', 1)
			    ->addAttributeToFilter('visibility', 4)
			    ->setOrder('position','DESC');

		Mage::getSingleton('cataloginventory/stock')
    		->addInStockFilterToCollection($productCollection);

    	return $productCollection;
	}

	public function getExplosion()
	{
		$store = Mage::app()->getStore();
		$collection = Mage::getModel('catalog/product')->getCollection()->setStoreId($store->getId())
			->addWebsiteFilter($store->getWebsiteId())
			->addAttributeToSelect('*')
			->addAttributeToFilter('tag', Silk_Qslb_Helper_Tag::EXPLOSION)
			->addAttributeToFilter('visibility', array(
				'neq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE))
			->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
			->setOrder('position');
		Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
		return $collection;
	}

	public function getNewProducts()
	{
		$productCollection = Mage::getModel('catalog/category')->load($this->newProductCategoryId)
				->getProductCollection()
			    ->addAttributeToSelect('*')
			    ->addAttributeToFilter('status', 1)
			    ->addAttributeToFilter('visibility', 4)
			    ->setOrder('position','DESC');
		Mage::getSingleton('cataloginventory/stock')
    		->addInStockFilterToCollection($productCollection);

    	return $productCollection;
	}

	public function getGuessProducts()
	{
		$customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();

		if(!$customerId) return false;

		$orders = Mage::getModel('sales/order')->getCollection()
					->addAttributeToSelect('*')
					->addAttributeToFilter('customer_id',$customerId)
					->setOrder('entity_id')->setPage(1,3);

		if(count($orders) <= 0) return false;

		$productIds = array();
		foreach($orders as $_order)
		{
		    $_items = $_order->getItemsCollection();
		    foreach($_items as $item)
		    {
		        $productIds[] = $item->getProductId();
		    }
		}

		$brandIds = array();
		foreach($productIds as $pid){
			$brandIds[] = Mage::getModel('catalog/product')->load($pid)->getBrand();
		}

		$brandIds = array_unique($brandIds);

		$productCollection = Mage::getModel("catalog/product")->getCollection()
		    ->addAttributeToSelect('*')
		    ->addWebsiteFilter(Mage::app()->getWebsite())
			->addAttributeToFilter('status', 1)
			->addAttributeToFilter('visibility', 4)
		    ->addAttributeToFilter('entity_id', array('nin'=>$productIds))
		    ->addAttributeToFilter('brand', array('in'=>$brandIds));

		$productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));

		$productCollection->setPage(1, 16);

		Mage::getSingleton('cataloginventory/stock')
    		->addInStockFilterToCollection($productCollection);
    		
		return $productCollection;
	}

}